/**
 * 
 */
package reuseplugin.views.downloadListe;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;

import reuseplugin.Activator;
import reuseplugin.ClientInterface;
import reuseplugin.actions.download.AddRatingAction;

import common.itemhandling.files.Download;

/**
 * @author Julius Ollesch
 * 
 */

public class DownloadView extends ViewPart {

	class NameSorter extends ViewerSorter {
	}

	class ViewContentProvider implements IStructuredContentProvider,
			ClientInterface {
		public void dispose() {
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * org.eclipse.jface.viewers.IStructuredContentProvider#getElements(
		 * java.lang.Object)
		 */
		public Object[] getElements(Object inputElement) {
			/**
			 * Abfrage ob Session aktiv
			 */
			if (Activator.getDefault().c.isAktiv()) {

				return c.getBenutzer().getDownloads().toArray();

			}
			return new Object[0];

		}

		public void inputChanged(Viewer v, Object oldInput, Object newInput) {
		}

	}

	class ViewLabelProvider extends LabelProvider implements
			ITableLabelProvider, ClientInterface {

		public Image getColumnImage(Object obj, int index) {
			return getImage(obj);
		}

		public String getColumnText(Object obj, int index) {
			Download download = (Download) obj;
			switch (index) {
			case 0:
				return download.getDatei().getName();
			case 1:
				return String.valueOf(download.getBewertung());
			default:
				throw new RuntimeException(
						reuseplugin.views.downloadListe.Messages.DownloadView_errorView);
			}

		}

		@Override
		public Image getImage(Object obj) {
			return PlatformUI.getWorkbench().getSharedImages().getImage(
					ISharedImages.IMG_ELCL_SYNCED);
		}
	}

	/**
	 * The ID of the view as specified by the extension.
	 */
	public static final String ID = "reuseplugin.views.DownloadView"; //$NON-NLS-1$

	private TableViewer viewer;

	private Action addRating;

	/*
	 * The content provider class is responsible for providing objects to the
	 * view. It can wrap existing objects in adapters or simply return objects
	 * as-is. These objects may be sensitive to the current input of the view,
	 * or ignore it and always show the same content (like Task List, for
	 * example).
	 */

	/**
	 * 
	 */
	public DownloadView() {
		
	}

	private void contributeToActionBars() {
		IActionBars bars = getViewSite().getActionBars();
		fillLocalPullDown(bars.getMenuManager());
		fillLocalToolBar(bars.getToolBarManager());
	}

	private void createColumns(final TableViewer viewer) {

		for (int i = 0; i < 2; i++) {
			final int index = i;
			final TableViewerColumn viewerColumn = new TableViewerColumn(
					viewer, SWT.NONE);
			final TableColumn column = viewerColumn.getColumn();
			switch (index) {
			case 0:
				column
						.setText(reuseplugin.views.downloadListe.Messages.DownloadView_labelFile);
				break;
			case 1:
				column
						.setText(reuseplugin.views.downloadListe.Messages.DownloadView_labelRating);
				break;

			default:
				throw new RuntimeException(
						reuseplugin.views.downloadListe.Messages.DownloadView_errorView);
			}

			column.setWidth(100);
			column.setResizable(true);
			column.setMoveable(true);

		}

		Table table = viewer.getTable();
		table.setHeaderVisible(true);
		table.setLinesVisible(false);
	}

	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	@Override
	public void createPartControl(Composite parent) {
		createViewer(parent);

		// Create the help context id for the viewer's control
		PlatformUI.getWorkbench().getHelpSystem().setHelp(viewer.getControl(),
				"ReusePlugin.viewer"); //$NON-NLS-1$
		makeActions();
		hookContextMenu();
		hookDoubleClickAction();
		contributeToActionBars();
		// Make the selection available
		getSite().setSelectionProvider(viewer);
		// Set the sorter for the table

	}

	private void createViewer(Composite parent) {

		viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
				| SWT.V_SCROLL | SWT.FULL_SELECTION);
		createColumns(viewer);
		viewer.setContentProvider(new ViewContentProvider());
		viewer.setLabelProvider(new ViewLabelProvider());
		viewer.setSorter(new NameSorter());
		viewer.setInput(getViewSite());

		// Layout the viewer
		GridData gridData = new GridData();
		gridData.verticalAlignment = GridData.FILL;
		gridData.horizontalSpan = 2;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.horizontalAlignment = GridData.FILL;
		viewer.getControl().setLayoutData(gridData);

	}

	private void fillContextMenu(IMenuManager manager) {
		manager.add(addRating);

		// Other plug-ins can contribute there actions here
		manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
	}

	private void fillLocalPullDown(IMenuManager manager) {
		manager.add(addRating);
	}

	private void fillLocalToolBar(IToolBarManager manager) {
		manager.add(addRating);

	}

	public TableViewer getViewer() {
		return viewer;
	}

	private void hookContextMenu() {
		MenuManager menuMgr = new MenuManager("#PopupMenu"); //$NON-NLS-1$
		menuMgr.setRemoveAllWhenShown(true);
		menuMgr.addMenuListener(new IMenuListener() {
			public void menuAboutToShow(IMenuManager manager) {
				DownloadView.this.fillContextMenu(manager);
			}
		});
		Menu menu = menuMgr.createContextMenu(viewer.getControl());
		viewer.getControl().setMenu(menu);
		getSite().registerContextMenu(menuMgr, viewer);
	}

	private void hookDoubleClickAction() {
		viewer.addDoubleClickListener(new IDoubleClickListener() {
			public void doubleClick(DoubleClickEvent event) {
				addRating.run();
			}
		});
	}

	private void makeActions() {

		addRating = new AddRatingAction(viewer);

		addRating.setText("Add Rating"); //$NON-NLS-1$
		addRating.setToolTipText("Adds a Rating to the selected Download"); //$NON-NLS-1$
		addRating.setImageDescriptor(PlatformUI.getWorkbench()
				.getSharedImages().getImageDescriptor(
						ISharedImages.IMG_OBJS_INFO_TSK));

	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	@Override
	public void setFocus() {
		viewer.getControl().setFocus();
		viewer.refresh(true);
	}

	private void showMessage(String message) {
		MessageDialog.openInformation(viewer.getControl().getShell(),
				"Download View", //$NON-NLS-1$
				message);
	}
}